package 多无人机架次;
/**
 * 方法名：TimeCount
 * 输入：车辆访问集合   无人机访问集合
 * 输出：当前车辆行驶时间。
 */

import GA_Test.GAData;

import java.util.ArrayList;

public class TimeCount {


    public static Double timeCount(ArrayList<Integer> RT,ArrayList<Integer> RU){

        //ReadDate.ReadNode();

        //节点间的距离集合
        //Double[][] distance = ReadDate.countDistance();
        Double truck_only_time=0.0;
        if(RU.size()==0){
            truck_only_time += GAData.disCollection[0][RT.get(0)];
            for(int i = 0;i<RT.size()-1;i++){
                 truck_only_time += GAData.disCollection[RT.get(i)][RT.get(i+1)];
             }
            truck_only_time += GAData.disCollection[RT.get(RT.size() - 1)][0];
        }else {
            int t_str1 = 0;
            int t_end1 = 0;
            int t_str2 = 0;
            int t_end2 = 0;
            int d_str1 = 0;
            int d_end1 = 0;
            int d_str2 = 0;
            int d_end2 = 0;
            mk:
            for (int i = 0; i < RU.size(); i++) {
                for (int k = 0; k < RT.size(); k++) {//和所有的RT内的数进行比较
                    if (RU.get(i) == RT.get(k)) {
                        t_str1 = k;
                        d_str1 = i;
                        break mk;
                    }
                }
            }
            a:
            for (int i = d_str1 + 1; i < RU.size(); i++) {//拿一个RU出来
                for (int k = t_str1 + 1; k < RT.size(); k++) {//和所有的RT内的数进行比较
                    if (RU.get(i) == RT.get(k)) {
                        t_end1 = k;
                        d_end1 = i;
                        break a;
                    }
                }
            }
            b:
            for (int i = d_end1 + 1; i < RU.size(); i++) {
                for (int k = t_end1 + 1; k < RT.size(); k++) {
                    if (RU.get(i) == RT.get(k)) {
                        t_str2 = k;
                        d_str2 = i;
                        break b;
                    }
                }
            }
            if (d_str2 != 0) {
                c:
                for (int i = d_str2 + 1; i < RU.size(); i++) {
                    for (int k = t_str2 + 1; k < RT.size(); k++) {
                        if (RU.get(i) == RT.get(k)) {
                            d_end2 = i;
                            t_end2 = k;
                            break c;
                        }
                    }
                }
            }

           truck_only_time = GAData.disCollection[0][RT.get(0)];
            Double first_period = 0.00;
            Double second_period = 0.00;
            for (int i = 0; i < t_str1; i++) {// 计算第一个起飞点之前 车辆的行驶时间
                truck_only_time += GAData.disCollection[RT.get(i)][RT.get(i + 1)];
            }

            if (t_end1 != 0) {
                //计算第一段时间
                Double che = 0.00;
                Double drone = 0.00;
                for (int i = t_str1; i < t_end1; i++) {
                    che += GAData.disCollection[RT.get(i)][RT.get(i + 1)];
                }
                for (int j = d_str1; j < d_end1; j++) {
                    drone += GAData.disCollection[RU.get(j)][RU.get(j + 1)];
                }
                first_period = (che > (drone * 2 / 3) ? che : drone * 2 / 3);
            }
            if (t_end2 != 0) {
                Double che = 0.00;
                Double drone = 0.00;
                for (int i = t_str2; i < t_end2; i++) {
                    che += GAData.disCollection[RT.get(i)][RT.get(i + 1)];
                }
                for (int j = d_str2; j < d_end2; j++) {
                    drone += GAData.disCollection[RU.get(j)][RU.get(j + 1)];
                }
                second_period = (che > (drone * 2 / 3) ? che : drone * 2 / 3);
            }
//         if(first_period == 0.00){
//             //此时说明没有无人机路径   因此只需要计算纯车辆路径
//             for(int i = 0;i<RT.size()-1;i++){
//                 truck_only_time += distance[RT.get(i)][RT.get(i+1)];
//             }
//         }
            if (second_period == 0.00) {
                //此时说明只有一个架次
                truck_only_time += first_period;
                for (int j = t_end1; j < RT.size() - 1; j++) {
                    truck_only_time += GAData.disCollection[RT.get(j)][RT.get(j + 1)];
                }
            } else {
                //此时说明有两个架次
                truck_only_time += first_period;
                for (int i = t_end1; i < t_str2; i++) {
                    truck_only_time += GAData.disCollection[RT.get(i)][RT.get(i + 1)];
                }
                truck_only_time += second_period;
                for (int j = t_end2; j < RT.size() - 1; j++) {
                    truck_only_time += GAData.disCollection[j][j + 1];
                }
            }
            truck_only_time += GAData.disCollection[RT.get(RT.size() - 1)][0];
        }
         return truck_only_time;
    }
}
